package com.jpetrak.gate.stringid;

import gate.Resource;
import gate.creole.AbstractLanguageResource;
import gate.creole.ResourceInstantiationException;
import gate.creole.metadata.CreoleParameter;
import gate.creole.metadata.Optional;
import gate.event.CreoleEvent;
import gate.event.CreoleListener;
import gate.gui.ActionsPublisher;
import gate.util.GateRuntimeException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.swing.AbstractAction;

/**
 * A LR representing a StringIdMap which can be used to map strings to id numbers and back.
 * The class also includes some static utility methods for converting id numbers to 
 * other representations and back.
 * 
 * @author Johann Petrak
 */
public class StringIdLR
  extends AbstractLanguageResource
  implements ActionsPublisher, CreoleListener{

  @Optional
  @CreoleParameter(
    comment="Directory for storing the persistent data of the StringIdMap",
    defaultValue = "")
  /**
   * Set the URL of the directory that contains the random index data or
   * should contain the random index data (if it is still to be created).
   */
  public void setDirectoryURL(URL directoryURL) {
    this.directoryURL = directoryURL;
  }
  public URL getDirectoryURL() {
    return directoryURL;
  }
  protected URL directoryURL;

  // FIELDS
  


  @Override
  public Resource init() throws ResourceInstantiationException {
    if(getDirectoryURL() == null || getDirectoryURL().equals("")) {
      try {
        directoryURL = createTmpDir();
      } catch (MalformedURLException ex) {
        throw new ResourceInstantiationException(
                "Could not create a tempory directory",ex);
      }
    }
    // check the directory and see if we have index files in there.
    // set the internal flags accordingly.
    File directory = gate.util.Files.fileFromURL(directoryURL);
    if(!directory.exists()) {
      throw new ResourceInstantiationException(
              "No directory exists with URL: "+directoryURL);
    }
    return this;
  }
  
  protected URL createTmpDir() throws MalformedURLException {
    File tmpDir = null;
    String tmpDirName = System.getProperty("java.io.tmpdir");
    String prefixWithTimeStamp = "StringIdLR" + Long.toString(System.currentTimeMillis(),36);
    for (int trial = 0; trial < 1000; trial++) {
      tmpDir = new File(tmpDirName, prefixWithTimeStamp + trial );
      if (tmpDir.mkdir()) {
        return tmpDir.toURI().toURL();
      }
    }
    throw new GateRuntimeException("Could not create temporary directory  "
       + tmpDir + " after 1000 unique retries"); 
  }

  


  // implement ActionsPublisher methods
  protected List<AbstractAction> actionsList;
  public List getActions() {
    return actionsList;
  }

  // ************ Creole Listener Methods ************
  public void resourceLoaded(CreoleEvent ce) {
    // do nothing
  }

  public void datastoreClosed(CreoleEvent ce) {
  }

  public void resourceRenamed(Resource resource, String oldname, String newname) {
  }

  public void datastoreOpened(CreoleEvent ce) {
  }

  public void datastoreCreated(CreoleEvent ce) {
  }

  public void resourceUnloaded(CreoleEvent ce) {
  }

}
